<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home', name: '' }">
        首页
      </el-breadcrumb-item>
      <el-breadcrumb-item>
        商品管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>
        商品分类
      </el-breadcrumb-item>
    </el-breadcrumb>

    <el-card shadow="always" :body-style="{ padding: '20px' }">
      <!-- 按钮区域 -->
      <el-row :gutter="20">
        <el-col :span="12" :offset="0">
          <el-button type="primary" size="default" @click="showAddCateDialog">
            添加分类
          </el-button>
        </el-col>
        <el-col :span="12" :offset="0"></el-col>
      </el-row>
      <!-- 表格区域 -->
      <tree-table
        class="tree-table"
        :data="cateList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        :show-index="true"
        index-text="#"
        border
      >
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="scope">
          <i
            class="el-icon-success"
            style="color:lightgreen;"
            v-if="scope.row.cat_deleted === false"
          >
          </i>
          <i class="el-icon-error" style="color:red;" v-else> </i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag size="normal" effect="light" v-if="scope.row.cat_level === 0">
            一级
          </el-tag>
          <el-tag
            type="success"
            size="normal"
            effect="light"
            v-else-if="scope.row.cat_level === 1"
          >
            二级
          </el-tag>
          <el-tag
            type="warning"
            size="normal"
            effect="light"
            v-else-if="scope.row.cat_level === 2"
          >
            三级
          </el-tag>
        </template>
        <!-- 操作 -->
        <template slot="option">
          <el-button
            size="mini"
            type="primary"
            style="margin-left: 10px"
            icon="el-icon-edit-outline"
          >
            编辑
          </el-button>
          <el-button
            type="danger"
            size="mini"
            style="margin-left: 10px"
            icon="el-icon-delete"
          >
            删除
          </el-button>
        </template>
      </tree-table>
      <!-- 分页区域 -->

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="querInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="querInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
        :pager-count="7"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      title="添加分类"
      :visible.sync="addCateDialogVisible"
      width="50%"
      @close="addCateDialogClosed"
    >
      <el-form
        :model="addCateFrom"
        ref="addCateFromRef"
        :rules="addCateFromRules"
        label-width="100px"
        :inline="false"
        size="normal"
      >
        <!-- prop 是对应验证规则 -->
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="addCateFrom.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类:">
          <el-cascader
            :options="parentCateList"
            v-model="selectedParentCate"
            clearable
            expand-trigger="hover"
            :show-all-levels="true"
            :props="cascaderProps"
            :change-on-select="true"
            @change="parentCateChange"
          >
          </el-cascader>
        </el-form-item>
      </el-form>

      <span slot="footer">
        <el-button @click="addCateDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="addCate">确认</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  created() {
    this.getCateList()
  },
  data() {
    return {
      cateList: [],
      querInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      columns: [
        { label: '分类名称', prop: 'cat_name' },
        { label: '是否有效', type: 'template', template: 'isok', prop: '' },
        { label: '排序', type: 'template', template: 'order' },
        { label: '操作', type: 'template', template: 'option' }
      ],
      total: 0,
      addCateDialogVisible: false,
      addCateFrom: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      addCateFromRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      parentCateList: [],
      selectedParentCate: [],
      // 指定级联选择器配置对象
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      }
    }
  },
  methods: {
    // 获取分类列表
    async getCateList() {
      const { data: res } = await this.$http.get('categories', {
        params: this.querInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品分类失败')
      }
      this.cateList = res.data.result
      this.total = res.data.total
    },
    showAddCateDialog() {
      this.getParentCateList()
      this.addCateDialogVisible = true
    },

    // pagesize改变
    handleSizeChange(newSize) {
      this.querInfo.pagesize = newSize
      this.getCateList()
    },
    // current page 改变
    handleCurrentChange(pageNum) {
      this.querInfo.pagenum = pageNum
      this.getCateList()
    },
    // 获取父类分类列表
    async getParentCateList() {
      const { data: res } = await this.$http.get('categories', {
        params: { type: 2 }
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取父类分类列表失败')
      }
      console.log(res)
      this.parentCateList = res.data
    },
    parentCateChange() {
      console.log(this.selectedParentCate)
      if (this.selectedParentCate.length > 0) {
        this.addCateFrom.cat_pid = this.selectedParentCate[
          this.selectedParentCate.length - 1
        ]
        this.addCateFrom.cat_level = this.selectedParentCate.length
      } else {
        this.addCateFrom.cat_pid = 0
        this.addCateFrom.cat_level = 0
      }
    },
    addCateDialogClosed() {
      this.$refs.addCateFromRef.resetFields()
      this.selectedParentCate = []
    },
    // 添加分类
    addCate() {
      console.log(this.selectedParentCate)
      this.$refs.addCateFromRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post(
          'categories',
          this.addCateFrom
        )
        if (res.meta.status !== 201) {
          return this.$message.error('添加失败')
        }
        this.$message.success('添加成功')
        this.getCateList()
        this.addCateDialogVisible = false
      })
    }
  }
}
</script>
<style lang="less" scoped>
.tree-table {
  top: 15px;
}
.el-cascader {
  width: 100%;
}
</style>
